package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseBaseDTO;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {


    @Override
    public PageVO queryCourseBaseList(PageRequestParams params, QueryCourseModel model, Long companyId) {

        //判断分页数据
        //判断pageNo是否为空
        if (params.getPageNo() < 1) {
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }
        //判断pageSize是否为空
        if (params.getPageSize() < 1) {
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }

        //判断查询条件
        LambdaQueryWrapper<CourseBase> qw = new LambdaQueryWrapper<>();

        //判断是否有查询课程名称条件
        if (StringUtil.isNotBlank(model.getCourseName())) {
            //有就添加条件
            qw.like(CourseBase::getName, model.getCourseName());
        }

        //判断是否有查询课程审核状态条件
        if (StringUtil.isNotBlank(model.getAuditStatus())) {
            //有就添加条件
            qw.eq(CourseBase::getAuditStatus, model.getAuditStatus());
        }

        //添加条件：当前机构只能查询自己机构发布的课程内容
        qw.eq(CourseBase::getCompanyId, companyId);

        //创建分页对象
        Page<CourseBase> page = new Page<>(params.getPageNo(), params.getPageSize());

        //查询分页数据
        Page<CourseBase> basePage = this.page(page, qw);

        //获取集合中的数据内容
        List<CourseBase> courseBaseList = basePage.getRecords();

        //获取总条数
        long total = basePage.getTotal();

        //使用mapstruct将pos转换成dtos
        List<CourseBaseDTO> courseBaseDTOList = Collections.emptyList();
        if (!CollectionUtils.isEmpty(courseBaseList)) {
            courseBaseDTOList = CourseBaseConvert.INSTANCE.entityToDtoList(courseBaseList);
        }

        //返回dto对象
        return new PageVO(courseBaseDTOList, total, params.getPageNo(), params.getPageSize());
    }
}
